package tool;

import org.apache.struts2.util.StrutsTypeConverter;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

public class DateTypeConverter extends StrutsTypeConverter {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @SuppressWarnings("rawtypes")
    @Override
    public Date convertFromString(Map map, String[] strings, Class aClass) {
        try {
            java.util.Date date = sdf.parse(strings[0]);
            if (date != null)
                return new java.sql.Date(date.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String convertToString(@SuppressWarnings("rawtypes") Map map, Object o) {
        return sdf.format(o);
    }
}
